package migrations

import (
	"time"

	"gorm.io/gorm"
)

// 添加全局变量存储迁移函数
var migrations = make(map[string]func(*gorm.DB) error)

// 添加AddMigration函数定义
func AddMigration(name string, fn func(*gorm.DB) error) {
	migrations[name] = fn
}

type RuleAction struct {
	ActionType string      `json:"action_type"`
	Target     string      `json:"target"`
	Parameters interface{} `json:"parameters"`
}

func init() {
	AddMigration("business_rules", func(tx *gorm.DB) error {
		type BusinessRule struct {
			ID            string       `gorm:"primaryKey"`
			TenantID      string       `gorm:"index"`
			RuleName      string       `gorm:"size:255"`
			ConditionExpr string       `gorm:"type:text"`
			Actions       []RuleAction `gorm:"type:json"`
			TriggerEvent  string       `gorm:"size:255"`
			Scope         string       `gorm:"size:50"`
			Status        bool
			CreatedAt     time.Time
			UpdatedAt     time.Time
		}
		return tx.AutoMigrate(&BusinessRule{})
	})
}
